//      config.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "config.h"

void config_load ( Explorer * e )
{
	gchar * config = g_build_filename ( g_get_home_dir (), ".system", "explorer.ini", null ) ;
	if ( ! g_file_test ( config, G_FILE_TEST_EXISTS ))
	{
		gchar * message = { "# Some Text" } ;
		g_file_set_contents ( config, message, strlen ( message ), null ) ;
		
		g_free ( config ) ;
		return ;
	}
	
	GKeyFile * file = g_key_file_new () ;
	GError * error = null ;
	
	g_key_file_load_from_file ( file, config, G_KEY_FILE_NONE, & error ) ;
	if ( error )
	{
		g_debug ( "%s %s", __func__, error -> message ) ;
		g_key_file_free ( file ) ;
	
		g_free ( config ) ;
		return ;
	}

	e -> config.click_mode = g_key_file_get_integer ( file, "browser", "click_mode", null ) ;
	if ( error )
	{
		e -> config.click_mode = 2 ;
		error = null ;
	}
	
	g_key_file_free ( file ) ;
	g_free ( config ) ;
}

void config_save ( Explorer * e )
{
	gchar * config = g_build_filename ( g_get_home_dir (), ".system", "explorer.ini", null ) ;
	if ( ! g_file_test ( config, G_FILE_TEST_EXISTS ))
	{
		gchar * message = { "# Some Text" } ;
		g_file_set_contents ( config, message, strlen ( message ), null ) ;
		
		g_free ( config ) ;
		return ;
	}
	
	GKeyFile * file = g_key_file_new () ;
	GError * error = null ;
	
	g_key_file_load_from_file ( file, config, G_KEY_FILE_NONE, & error ) ;
	if ( error )
	{
		g_debug ( "%s %s", __func__, error -> message ) ;
		g_key_file_free ( file ) ;
	
		g_free ( config ) ;
		return ;
	}

	g_key_file_set_integer ( file, "browser", "click_mode", e -> config.click_mode ) ;
	april_key_file_write ( config, file ) ;
	
	g_key_file_free ( file ) ;
	g_free ( config ) ;
}
		
